package com.pesking.core.sqlparser.processor;

import java.util.Arrays;
import java.util.List;

import com.pesking.core.SpringBeanUtils;
import org.apache.commons.lang3.StringUtils;

import com.pesking.core.parameter.Parameter;
import com.pesking.core.sqlparser.parameter.SqlParameter;
import com.pesking.core.sqlparser.parameter.SqlParameterProcessor;

public class BeanMethodSqlParameterHandler extends SqlParameterProcessor{

	@Override
	public List<String> getSqlParameterType() {
		return Arrays.asList(new String []{"BEAN_METHOD"});
	}

	@Override
	public void handle(StringBuilder sbSQL, Parameter parameter, List<SqlParameter> processors,
			SqlParameter sqlParameter) {
		String className = sqlParameter.getName();
		String methodName = sqlParameter.getValue();
		if (StringUtils.isBlank(className) || StringUtils.isBlank(methodName)) {
			throw new RuntimeException("使用方法[BEAN_METHOD:接口路径:方法名称]");
		}
		Object rtn = SpringBeanUtils.getBeanMethodValue(className, methodName);
		String result = null;
		if (rtn != null) {
			result = rtn.toString();
		}
		else {
			result = "";
		}
		sbSQL.append(result);
	}

}
